<template>
  <div class="login-container">
    <div class="login-left">
      <img src="../assets/imgs/login_img.png" alt="">
    </div>
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/imgs/login_logo.png" alt="">
      </div>
      <div class="title">
        电商后台管理系统
      </div>
      <div class="login-form">
        <el-form ref="loginFromRef" :model="loginForm" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="请输入密码" prefix-icon="iconfont icon-3702mima"
              show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login-btn" @click="login">登陆</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据对象
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 表单检验规则
      rules: {
        username: [
          { required: true, message: "请输入账户名称", trigger: "blur" },
          { min: 3, max: 8, message: "长度在 3 到 8 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入账户密码", trigger: "blur" },
          { min: 6, max: 12, message: "长度在 6 到 12 个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods:{
      login(){
          this.$refs.loginFromRef.validate(async (valid) => {
            //  判断是否校验成功
            if(!valid){
                return
            }
            // 发送请求，判断是否登陆成功
            const {data:res} = await this.$http.post('login',this.loginForm)
            if(res.meta.status !== 200){
                return this.$message.error('登陆失败')
            }

            this.$message.success('登陆成功')
            // 将用户信息保存到sessionStorage中
            sessionStorage.setItem('userInfo',JSON.stringify(res.data))
            this.$router.push('/home')
          })
      }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  background: #93defe;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-left {
  margin-right: 100px;
}
.login-box {
  width: 400px;
  height: 350px;
  background: #fff;
  padding: 50px;
  border-radius: 6px;
  box-sizing: border-box;
  position: relative;

  .avatar-box {
    position: absolute;
    top: -60px;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    border: 5px solid #93defe;
    border-radius: 100px;
    background: #fff;
    text-align: center;
    line-height: 110px;
  }

  .title {
    position: absolute;
    top: 100px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;
    color: #444;
  }

  .login-form {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 80%;
    padding: 0 20px;
    box-sizing: border-box;

    .login-btn {
      width: 100%;
    }
  }
}
</style>